package com.czl.module_storehouse.activity.base;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.utils.PopWindowUtils;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.mvp.presenter.StorehouseLocationPresenter;
import com.czl.module_storehouse.mvp.view.StorehouseLocationView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author by xubin,
 * @date on 2022/2/21.
 * PS: Not easy to write code, please indicate.
 */
public abstract class BaseOperationLocationActivity<V extends ViewBinding> extends BaseActivity<V> implements StorehouseLocationView {
    private static final int TAG_BUILD = 0;
    private static final int TAG_AREA = 1;
    private static final int TAG_FLOOR = 2;
    protected int tag = TAG_BUILD;
    private boolean showLocationList;
    /**
     * 已选的位置
     */
    protected final HashSet<Integer> mSelectedSet = new HashSet<>();

    protected List<LocatListBean> mStoreHouseBeanList;

    protected void getStorehouseLocationList() {
        if (getLocationPresenter() != null) {
            getLocationPresenter().getStorehouseLocatList();
        }
    }

    /**
     * 获取presenter
     *
     * @return
     */
    protected abstract StorehouseLocationPresenter getLocationPresenter();

    /**
     * 选择库位
     */
    protected void getStorehouseLocatList() {
        getStorehouseLocatList(false);
    }

    protected void getStorehouseLocatList(boolean show) {
        this.showLocationList = show;
        if (mStoreHouseBeanList == null) {
            getStorehouseLocationList();
        } else {
            showPopupWindowLocation();
        }
    }

    protected void showPopupWindowLocation() {
        tag = TAG_BUILD;
        if (mStoreHouseBeanList == null || mStoreHouseBeanList.size() == 0) {
            showToast("该仓库没有设置库位");
            return;
        }

        List<LocatListBean> list = createAreas(null);
        View view = View.inflate(getContext(), R.layout.common_recycler_view, null);

        RecyclerView recyclerView = view.findViewById(R.id.rv_item);
        BaseQuickAdapter<LocatListBean, BaseViewHolder> adapter = new BaseQuickAdapter<LocatListBean, BaseViewHolder>(R.layout.item_select_type, list) {

            @Override
            protected void convert(@NonNull BaseViewHolder helper, LocatListBean item) {
                helper.setText(R.id.tv_name, item.getStorehouseLocatName());
            }
        };
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            private List<LocatListBean> mChildList;
            private List<LocatListBean> newList;

            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (TAG_BUILD == tag) {
                    tag = TAG_AREA;
                    LocatListBean bean = mStoreHouseBeanList.get(position);
                    mChildList = bean.getChildList();
                    if (mChildList == null || mChildList.size() == 0) {
                        PopWindowUtils.getInstance().dismissPopupWindow();
                        callBackModel(bean);
                        return;
                    }
                    newList = createAreas(bean);
                    adapter.setList(newList);
                } else if (TAG_AREA == tag) {
                    tag = TAG_FLOOR;
                    LocatListBean bean = mChildList.get(position);
                    List<LocatListBean> childList = bean.getChildList();
                    if (childList == null || childList.size() == 0) {
                        PopWindowUtils.getInstance().dismissPopupWindow();
                        callBackModel(bean);
                        return;
                    }
                    newList = createAreas(bean);
                    adapter.setList(newList);
                } else {
                    LocatListBean bean = newList.get(position);
                    PopWindowUtils.getInstance().dismissPopupWindow();
                    callBackModel(bean);
                }
            }
        });

        int width = ScreenUtils.getScreenWidth() - SizeUtils.dp2px(30);
        if (list.size() > 6) {
            int height = (int) (ScreenUtils.getScreenHeight() * 0.6f);
            PopWindowUtils.getInstance().createPopWindow(getContext(), view, width, height, "位置选择");
        } else {
            PopWindowUtils.getInstance().createPopWindow(getContext(), view, width, "位置选择");
        }
        PopWindowUtils.getInstance().showBasePopupWindow(binding.getRoot());
    }

    private List<LocatListBean> createAreas(LocatListBean storeHouseBean) {
        List<LocatListBean> list = new ArrayList<>();
        if (TAG_BUILD == tag) {
            for (LocatListBean bean : mStoreHouseBeanList) {
                boolean contains = mSelectedSet.contains(bean.getStorehouseLocatId());
                list.add(bean);
//                if (!contains) {
//                }
            }
        } else {
            List<LocatListBean> childList = storeHouseBean.getChildList();
            if (childList == null) {
                return list;
            }
            for (LocatListBean bean : childList) {
                boolean contains = mSelectedSet.contains(bean.getStorehouseLocatId());
                list.add(bean);
//                if (!contains) {
//                }
            }
        }
        return list;
    }

    protected boolean callBackModel(LocatListBean bean) {
        if (bean == null) {
            return false;
        }
        return mSelectedSet.add(bean.getStorehouseLocatId());
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        if (StorehouseLocationPresenter.TAG_GET_LOCATION_LIST.equals(httpResponse.getRequestTag())) {
            ListBean<LocatListBean> listBean = (ListBean<LocatListBean>) httpResponse.getData();
            mStoreHouseBeanList = listBean.getList();
            if (showLocationList) {
                showPopupWindowLocation();
            }
        }
    }
}
